package com.cpr.core.rabbitmq.client;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;

/**
 * @author Chen Panrui
 * created at 2022/3/10 13:59
 * mq,拉取消息的模式
 */
public class HelloGetConsumer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 从哪个消息队列消费，是否自动确认消息
        // 通过 consume的方式获取 队列里面的消息，队列里面的消息还会在的，但是通过get的方式获取 队列里面的消息就会拿走不见了
        GetResponse getResponse = channel.basicGet("queue.biz", true);

        byte[] body = getResponse.getBody();
        System.out.println("消息:" + new String(body));

        //AMQP.BasicProperties props = getResponse.getProps();
        channel.close();
        connection.close();
    }
}
